package com.qxzn.module.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qxzn.app.bean.EmergencyContactBean;
import com.qxzn.module.mine.R;

import java.util.ArrayList;
import java.util.List;

import cn.we.swipe.helper.WeSwipeHelper;


public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.RecViewholder> {
    private Context context;
    private List<EmergencyContactBean> data = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private DeletedItemListener delectedItemListener;
    private EditItemListener editItemListener;

    public void setDelectedItemListener(DeletedItemListener deletedItemListener) {
        this.delectedItemListener = deletedItemListener;
    }

    public void setEditItemListener(EditItemListener editItemListener) {
        this.editItemListener = editItemListener;
    }

    public ContactListAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<EmergencyContactBean> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public List<EmergencyContactBean> getData() {
        return data;
    }

    public void removeDataByPosition(int position) {
        if (position >= 0 && position < data.size()) {
            data.remove(position);
            notifyItemRemoved(position);
        }
    }

    @Override
    public RecViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.contact_item_layout, parent, false);
        return new RecViewholder(view);
    }

    @Override
    public void onBindViewHolder(final RecViewholder holder, int position) {
        EmergencyContactBean contactBean = data.get(position);
        holder.itemView.setOnClickListener(null);
        holder.name.setText(contactBean.name);
        holder.phone.setText(contactBean.phone);
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != editItemListener) {
                    editItemListener.edit(holder.getAdapterPosition());
                }
            }
        });
        holder.shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != delectedItemListener) {
                    delectedItemListener.deleted(holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    /**
     * view.getWidth()获取的是屏幕中可以看到的大小.
     */
    public class RecViewholder extends RecyclerView.ViewHolder implements WeSwipeHelper.SwipeLayoutTypeCallBack {
        public View space;
        public TextView edit, shanchu;
        public RelativeLayout slideItem;
        public TextView name;
        public TextView phone;

        public RecViewholder(View itemView) {
            super(itemView);
            space = itemView.findViewById(R.id.space);
            edit = itemView.findViewById(R.id.edit);
            shanchu = itemView.findViewById(R.id.shanchu);
            slideItem = itemView.findViewById(R.id.slide_itemView);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
        }

        @Override
        public float getSwipeWidth() {
            //布局隐藏超过父布局的范围的时候这里得不到宽度
            return dip2px(context, 160);
        }

        @Override
        public View needSwipeLayout() {
            return slideItem;
        }

        @Override
        public View onScreenView() {
            return space;
        }
    }

    /**
     * 根据手机分辨率从DP转成PX
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface DeletedItemListener {

        void deleted(int position);
    }

    public interface EditItemListener {

        void edit(int position);
    }
}
